package io.gitee.yxsnake.embark.web.core.exception;

import io.gitee.yxsnake.embark.web.core.base.ErrorCode;
import io.gitee.yxsnake.embark.web.core.base.IResultCode;
import io.gitee.yxsnake.embark.web.core.base.Result;
import io.gitee.yxsnake.embark.web.core.context.TrackContext;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.io.Serial;

/**
 * @author snake
 * @description 微信开放平台对接异常
 * @since 2024/10/24 0:25
 */
@Data
@ToString
@EqualsAndHashCode(callSuper = true)
public class WechatException extends RuntimeException {

    @Serial
    private static final long serialVersionUID = -2883953046265822542L;

    public ErrorCode errorCode;

    public WechatException(ErrorCode errorCode) {
        super(errorCode.getMsg());
        this.errorCode = errorCode;
    }

    public WechatException(String message) {
        super(message);
        this.errorCode = ErrorCode.builder()
                .code(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)
                .msg(message)
                .build();
    }

    public WechatException(Integer code, String message) {
        ErrorCode errorCode = ErrorCode.builder()
                .code(code)
                .msg(message)
                .build();
        this.errorCode = errorCode;
    }

    public WechatException(String message, Throwable cause) {
        super(message, cause);
    }

    public WechatException(Throwable cause) {
        super(cause);
    }

    public Result getResult() {
        return Result.builder()
                .code(this.errorCode.getCode())
                .msg(this.errorCode.getMsg())
                .trackId(TrackContext.getTrackId())
                .build();
    }

    public WechatException(IResultCode resultCode) {
        super(resultCode.getMsg());
        this.errorCode = ErrorCode.builder()
                .code(resultCode.getCode())
                .msg(resultCode.getMsg())
                .build();
    }
}
